//package me.fanchaoo.web.aspect;
//
//import com.nimbusds.jose.JWSObject;
//import com.nimbusds.jose.JWSVerifier;
//import com.nimbusds.jose.crypto.MACVerifier;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import me.fanchaoo.constant.JwtConstant;
//import me.fanchaoo.enums.EnumResponseCode;
//import me.fanchaoo.exception.BusinessException;
//import me.fanchaoo.web.dto.ApiLogDTO;
//import me.fanchaoo.web.util.IpUtils;
//import net.minidev.json.JSONObject;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.text.ParseException;
//
///**
// * @author
// * @date
// */
//@Aspect
//@Slf4j
//@Component
//public class ApiLogAspect {
//
//    @Autowired
//    private MongoTemplate mongoTemplate;
//
//    @Autowired
//    private IpUtils ipUtils;
//
//    private static final String TOKEN = "token";
//
//    @Pointcut("execution(public * me.fanchaoo.web.controller.*Controller.*(..))")
//    public void apiLogPointcut() {
//    }
//
//    @Around("apiLogPointcut()")
//    public Object around(ProceedingJoinPoint joinPoint) throws ParseException {
//
//        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
//        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
//        String operation = apiOperation.value();
//
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
//        HttpServletRequest request = servletRequestAttributes.getRequest();
//        String url = request.getRequestURI();
//        String userId = "noLogin";
//        String artistId = "noLogin";
//        String  ipAddress = ipUtils.getIpAddress(request);
//        String  ipSource =  ipUtils.getIpSource(ipAddress);
//      try {
//      String token = request.getHeader(TOKEN);
//      JWSObject jwsObject = JWSObject.parse(token);
//      JSONObject payload = jwsObject.getPayload().toJSONObject();
//      userId =payload.getAsString("userId");
//      artistId =payload.getAsString("artistId");
//      } catch (Exception e) {
//      }
//        Object result = null;
//        ApiLogDTO apiLogDTO = ApiLogDTO.builder()
//                .uri(url)
//                .operation(operation)
//                .artistId(artistId)
//                .userId(userId)
//                .ipAddress(ipAddress)
//                .ipSource(ipSource)
//                .userName("")
//                .build();
//        mongoTemplate.save(apiLogDTO);
//        try {
//            result = joinPoint.proceed();
//        } catch (Throwable throwable) {
////            throw new RuntimeException(throwable);
//        } finally {
////            Query query = new Query(Criteria.where("id").is(apiLogDTO.getId()));
////            Update update = new Update().set("userName", "");
////            mongoTemplate.updateFirst(query, update, ApiLogDTO.class);
//        }
//
//        return result;
//    }
//
//}
